package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.modules.sys.entity.SysDictEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import io.gbznt.modules.sys.service.SysDictService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussRtuWarningRecordDao;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussMaintainTaskEntity;
import io.gbznt.modules.buss.entity.BussRtuEquipmentEntity;
import io.gbznt.modules.buss.entity.BussRtuInfoEntity;
import io.gbznt.modules.buss.entity.BussRtuWarningRecordEntity;
import io.gbznt.modules.buss.entity.BussRtuWarningRoleEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussMaintainTaskService;
import io.gbznt.modules.buss.service.BussRtuEquipmentService;
import io.gbznt.modules.buss.service.BussRtuInfoService;
import io.gbznt.modules.buss.service.BussRtuWarningRecordService;
import io.gbznt.modules.buss.service.BussRtuWarningRoleService;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysUserService;

@Service("bussRtuWarningRecordService")
public class BussRtuWarningRecordServiceImpl extends ServiceImpl<BussRtuWarningRecordDao, BussRtuWarningRecordEntity>
		implements BussRtuWarningRecordService {
	@Autowired
	private BussFarmlandInfoService bussFarmlandInfoService;
	@Autowired
	private BussRtuEquipmentService bussRtuEquipmentService;
	@Autowired
	private BussRtuWarningRoleService bussRtuWarningRoleService;
	@Autowired
	private BussRtuInfoService bussRtuInfoService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private BussMaintainTaskService bussMaintainTaskService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String name = (String) params.get("name");// 田块
		String name1 = (String) params.get("name1");// 告警类型

		IPage<BussRtuWarningRecordEntity> page = this.page(new Query<BussRtuWarningRecordEntity>().getPage(params),
				new QueryWrapper<BussRtuWarningRecordEntity>()
						.eq(StringUtils.isNotBlank(name), "farmland_info_id", name)
						.eq(StringUtils.isNotBlank(name1), "warning_type", name1)
						// 个人数据权限控制
						.in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()), "dept_id",
								sysDeptService.getPermissionDeptIdsArr(true, true))
						.orderByDesc("create_time"));
		for (BussRtuWarningRecordEntity rrwre : page.getRecords()) {
			// 田块
			if (StringUtils.isNotBlank(rrwre.getFarmlandInfoId())) {
				BussFarmlandInfoEntity bussFarmlandInfo = bussFarmlandInfoService.getById(rrwre.getFarmlandInfoId());
				rrwre.setTkName(bussFarmlandInfo.getName());
			}
			// 设备
			if (StringUtils.isNotBlank(rrwre.getEquipmentId())) {
				BussRtuEquipmentEntity bree = bussRtuEquipmentService.getById(rrwre.getEquipmentId());
				if (bree != null && StringUtils.isNotBlank(bree.getName())) {
					rrwre.setEquipmentName(bree.getName());
				}
			}

			// 预警规则
			if (StringUtils.isNotBlank(rrwre.getWarningRoleId())) {
				BussRtuWarningRoleEntity bussRtuWarningRole = bussRtuWarningRoleService
						.getById(rrwre.getWarningRoleId());
				if (bussRtuWarningRole != null && StringUtils.isNotBlank(bussRtuWarningRole.getName())) {
					rrwre.setWarningRoleName(bussRtuWarningRole.getName());
				}
			}

			// rtu
			if (StringUtils.isNotBlank(rrwre.getRtuId())) {
				BussRtuInfoEntity rtu = bussRtuInfoService.getById(rrwre.getRtuId());
				if (rtu != null && StringUtils.isNotBlank(rtu.getSerialNum())) {
					rrwre.setRtuName(rtu.getSerialNum());
				}
			}
			// 接收人
			if (StringUtils.isNotBlank(rrwre.getReceiveD())) {
				SysUserEntity user = sysUserService.getById(rrwre.getReceiveD());
				if (user != null && StringUtils.isNotBlank(user.getName())) {
					rrwre.setReceiveName(user.getName());
				}
			}

			// 告警级别
			if (StringUtils.isNotBlank(rrwre.getGjjlJson())) {
				// json数据集
				JSONObject json = JSONObject.fromObject(rrwre.getGjjlJson());
				// 字典id
				String zdId = (String) json.get("id");
				SysDictEntity dict = sysDictService.getById(zdId);

				if (null != dict) {
					rrwre.setWarningLevName(dict.getValue());
				}
			}
			// 是否已生成工单
			QueryWrapper<BussMaintainTaskEntity> gdQw = new QueryWrapper<>();
			gdQw.eq("warning_record_id", rrwre.getId());
			int count = bussMaintainTaskService.count(gdQw);
			rrwre.setYnGongdan(count);

		}
		return new PageUtils(page);
	}

	@Override
	public List<BussRtuWarningRecordEntity> getGjjlMax(Map<String, Object> params) {
		List<BussRtuWarningRecordEntity> list = baseMapper.getGjjlMax(params);
		return list;
	}

}
